package DAO;

import hibernate.AssetSaleInfo;
import hibernateUtil.HibernateUtil;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZCCS {
	
	private int totalByQuery = 0;
	
	/*
	 * save资产信息
	 */
    public void addAssetSale(String parameter[]){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetSaleInfo assetSaleInfo = new AssetSaleInfo();
		
		Date purchaseTime = java.sql.Date.valueOf(parameter[2]);
		double money = Double.parseDouble(parameter[5]);
		
		assetSaleInfo.setAssetNo(parameter[0]);
		assetSaleInfo.setPurchaseUnit(parameter[1]);
		assetSaleInfo.setPurchaseTime(purchaseTime);
		assetSaleInfo.setStatus(parameter[3]);
		assetSaleInfo.setName(parameter[4]);
		assetSaleInfo.setMoney(money);
		assetSaleInfo.setContactMode(parameter[6]);
		assetSaleInfo.setEquipmentAttach(parameter[7]);
		assetSaleInfo.setRemark(parameter[8]);

		session.save(assetSaleInfo);
		trans.commit();
    }	
    
    /*
     * 得到资产出售信息的总记录
     */
    public int getTotal()
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		int countTotal = (session.createQuery("from AssetSaleInfo").list()).size(); //资产信息的总数;
		
		trans.commit();
		
		return countTotal;
    } 
    
    /*
     * 查询的资产信息的总记录
     * 参数 -- 
     */
    public int getTotalByQuery()
    {
    	return totalByQuery;
    }    
    
    @SuppressWarnings("unchecked")    
    /*
     * 通过参数得到资产出售的所有信息
     * 参数 start ， limit
     */
	public List getAllAssetSale(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();

		Query query = session.createQuery("from AssetSaleInfo");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		
		trans.commit();
		
		return l;
    }    
    
    @SuppressWarnings("unchecked")
    /*
     * 通过查询条件得到符合条件的资产信息
     * 参数 --- start ,limit ,assetNo , assetName , purchaseUnit
     *         purchaseStartTime , purchaseEndingTime
     */
    public List getAssetSaleByQuery(int start,int limit,String assetNo,String assetName,String purchaseUnit,String purchaseStartTime,String purchaseEndingTime)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetSaleInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(assetName))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or name=:assetName ";
			}
			else
			{
				str = str + "name=:assetName ";
			}
		}
		if(!"".equals(purchaseUnit))
		{
			if(!"".equals(assetNo) || !"".equals(assetName))
			{
				str = str + "or purchaseUnit=:purchaseUnit ";
			}
			else
			{
				str = str + "purchaseUnit=:purchaseUnit ";
			}
		}	
		if((!"".equals(purchaseStartTime)) && (!"".equals(purchaseEndingTime)))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(purchaseUnit))
			{
				str = str + "or purchaseTime between:startTime and:EndTime ";
			}	
			else
			{
				str = str + "purchaseTime between:startTime and:EndTime ";
			}			
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(assetName))
		{
			query.setString("assetName", assetName);
		}
		if(!"".equals(purchaseUnit))
		{
			query.setString("purchaseUnit", purchaseUnit);
		}		
		if(!"".equals(purchaseStartTime))
		{
			Date startUseTime_1 = java.sql.Date.valueOf(purchaseStartTime);
			query.setDate("startTime", startUseTime_1);
		}
		if(!"".equals(purchaseEndingTime))
		{
			Date EndTime_1 = java.sql.Date.valueOf(purchaseEndingTime);
			query.setDate("EndTime", EndTime_1);
		}		
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产信息的记录总数
		
		query.setFirstResult(start);    //查询记录从这一条开始；
		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }    

    @SuppressWarnings("unchecked")
    /*
     * 通过资产编号（assetNo）删除资产信息
     */
	public void deleteAssetSale(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			AssetSaleInfo assetSaleInfo = (AssetSaleInfo)session.get(AssetSaleInfo.class, assetNo.elementAt(i).toString());
			session.delete(assetSaleInfo);
		}
		
		trans.commit();
    } 
    
    /*
     * 通过资产编号(assetNo)更新资产出售信息
     * 参数 -- 除了资产状态和资产编号其他都可以更新
     */
    public void updataAssetSale(String parameter[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		System.out.println(parameter[0]);
		AssetSaleInfo assetSaleInfo = (AssetSaleInfo)session.get(AssetSaleInfo.class, parameter[0].toString());
		
		Date purchaseTime = java.sql.Date.valueOf(parameter[2]);
		double money = Double.parseDouble(parameter[5]);
		
		
		assetSaleInfo.setPurchaseUnit(parameter[1]);
		assetSaleInfo.setPurchaseTime(purchaseTime);
		assetSaleInfo.setStatus(parameter[3]);
		assetSaleInfo.setName(parameter[4]);
		assetSaleInfo.setMoney(money);
		assetSaleInfo.setContactMode(parameter[6]);
		assetSaleInfo.setEquipmentAttach(parameter[7]);
		assetSaleInfo.setRemark(parameter[8]);
		
		session.update(assetSaleInfo);
		
		trans.commit();
    }    
}
